Skip to content

Vite TS 配置alias(设置别名)

修改vite配置

ts
import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

const pathSrc = path.resolve(__dirname, 'src')

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '~/': `${pathSrc}/`,
    },
  },
})

修改tsconfig.json

json
{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "esnext",
    "useDefineForClassFields": true,
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"],
    "paths": {
      "~/*": ["src/*"]
    },
    "skipLibCheck": true
  },
  "vueCompilerOptions": {
    "target": 3,
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}

如果报错,确认一下是否安装path包等依赖

Released under the MIT License.